/* * * * M A P V I S I O N   L I C E N S E  *****************************************************************
         *
         * This MapVision software is © Filip Zavadil 2013. It is licensed under the LGPL license.
         * This MapVision software dynamically links to unmodified Qt5 Library.
         *
         * The Qt5 Library is © 2013 Qt® and/or its subsidiary(-ies), and is licensed under the
         * GNU Lesser General Public License version 2.1 with Qt® LGPL exception version 1.1.
         * Qt5 library is free software; you can redistribute it and/or modify it under the terms of the
         * GNU Lesser General Public License, version 2.1, as published by the Free Software Foundation.
         * Qt5 library is provided “AS IS”, without WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR
         * IMPLIED INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
         * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
         *
         * As an additional permission to the GNU Lesser General Public License version 2.1, the object
         * code form of a “work that uses the Library” may incorporate material from a header file that is
         * part of the Library. You may distribute such object code under terms of your choice,
         * provided that: (i) the header files of the Library have not been modified; and (ii)
         * the incorporated material is limited to numerical parameters, data structure layouts, accessors,
         * macros, inline functions and templates; and (iii) you comply with the terms of Section 6 of the
         * GNU Lesser General Public License version 2.1.
         *
         * Moreover, you may apply this exception to a modified version of the Library, provided that such
         * modification does not involve copying material from the Library into the modified Library’s header
         * files unless such material is limited to (i) numerical parameters; (ii) data structure layouts;
         * (iii) accessors; and (iv) small macros, templates and inline functions of five lines or less in length.
         *
         * Furthermore, you are not required to apply this additional permission to a modified version of the Library.
         *
         * You should have received a copy of the GNU Lesser General Public License along with this package;
         * if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

         * The source code for Qt 5.X SDK is available from Qt Project here:
         * http://download.qt-project.org/official_releases/qtcreator/2.7/2.7.0/qt-creator-2.7.0-src.zip.
         *
         * ****************************** M A P V I S I O N    S O F T W A R E  ************************************
         *
         * Author:       Filip Zavadil
         * Description:  Software for displaying and exporting map tiles.
         * The sources and executables of MapVision software are available:
         *    - from project hosting on CodePlex.com :   http://mapvision.codeplex.com
         *    - from GIT repositories on Gitorious.org:  https://gitorious.org/mapvision
         * Visit MapVision fanpage on Facebook.com:      https://www.facebook.com/pages/Mapvision/631827753513935
         *
         * *********************************************************************************************************
*/
#include "layer.h"

Layer::Layer(QMainWindow * mainWindow)
{
    zValue = 0;
    layerName = QString("no_name");
    source = QString("no_source");
    visible = true;
    YaxisOrigin = ZavaProj::TOP;
    opacity=1;

    if (settings.value("main/rendering").toString()=="CONCURRENT") renderingType = CONCURRENT;
    else if (settings.value("main/rendering").toString()=="CURRENT") renderingType = CURRENT;
    else renderingType = AUTO;

    layerGroup = new QGraphicsItemGroup;
    layerGroup->setData(Qt::UserRole,"layer");

    mainWindowPrivate=mainWindow;

    connect(getMapView(),SIGNAL(mapRectChanged(QRect,int)),this,SLOT(mapRectChanged(QRect,int)));
    connect(this,SIGNAL(sendMessage(QString)),mainWindow,SLOT(printMessage(QString)));
    connect(getMapView(),SIGNAL(zoomChanged(int)),this,SLOT(zoomChanged(int)));
    connect(getMapView(),SIGNAL(sceneCleared()),this,SLOT(sceneHasCleared()));

    getMapView()->setPanEnabled(true);
    id = getListWidget()->getId();
    dialog = new DialogPreferencesGlobal(this,this);
    tilesToLoad = 0;

    if (getMapView()->getWelcome()->isVisible()){
        getMapView()->getWelcome()->removeWelcome();
    }


}

Layer::~Layer()
{
    this->deleteLater();
}

void Layer::setVisible(bool isVisible)
{
    layerGroup->setVisible(isVisible);
    this->visible=isVisible;
}


void Layer::changeZvalue(int zVal)
{
    if (zVal!=getZvalue()){
        this->setZvalue(zVal);
        layerGroup->setZValue(zVal);
    }

}


bool Layer::isRendering()
{
    QCheckBox * checkBox = mainWindowPrivate->findChild<QCheckBox*>("checkBoxRender");
    return (checkBox->checkState() == Qt::Checked) ? true : false;
}


MapView * Layer::getMapView()
{
    return mainWindowPrivate->findChild<MapView *>("map");
}

ListWidget * Layer::getListWidget()
{
    return mainWindowPrivate->findChild<ListWidget*>("listWidgetLayers");
}

void Layer::changeOpacity(qreal opacity_)
{
    layerGroup->setOpacity(opacity_);
    opacity=opacity_;
}

void Layer::setLayerType(QString typeString)
{
    type = getLayerTypeFromString(typeString);
}

Layer::layerType Layer::getLayerTypeFromString(QString typeString)
{
    if (typeString.contains("TMSlocal")){
        return TMSlocal;
    } else if (typeString.contains("TMSonline")){
        return TMSonline;
    } else if (typeString.contains("WMTSlocal")){
        return WMTSlocal;
    } else if (typeString.contains("WMTSonline")){
        return WMTSonline;
    } else if (typeString.contains("MBtiles")){
        return MBtiles;
    } else if (typeString.contains("GeoImage")){
        return GeoImage

;
    }

    return UNKNOWN;
}

QString Layer::getLayerTypeString(layerType type_)
{
    if (type_ == TMSlocal){
        return "TMSlocal";
    } else if (type_ == TMSonline){
        return "TMSonline";
    } else if (type_ == WMTSlocal){
        return "WMTSlocal";
    } else if (type_ == WMTSonline){
        return "WMTSonline";
    } else if (type_ == MBtiles){
        return "MBtiles";
    } else if (type_ == GeoImage){
        return "GeoImage";
    }

    return "UNKNOWN";
}

void Layer::setTilesToLoad(int num)
{
    tilesToLoad+=num;

    if (tilesToLoad<=0) emit layerReady(true);
    else emit layerReady(false);
}

void Layer::getPreferences()
{
    if (!dialog->isVisible()){
        dialog->showDialog();
    } else {
        dialog->activateWindow();
    }
}

void Layer::sceneHasCleared()
{
    emit zoomIsChanging(getMapView()->getZoom());
    sources.clear();
    foreach (QGraphicsItem* item, items) {
        layerGroup->removeFromGroup(item);
        delete item;
    }
    items.clear();
    tilesToLoad=0;
}


void Layer::zoomChanged(int zoom)
{
    Q_UNUSED(zoom);

}
